/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.game.world;

import javax.media.opengl.GL3bc;
import jinngine.geometry.Geometry;
import jinngine.geometry.UniformCapsule;
import jinngine.math.Matrix3;
import jinngine.math.Vector3;
import jinngine.physics.Body;
import jinngine.physics.Scene;
import space.gl.RenderContext;
import space.model.Actor;
import space.model.Drawable;

/**
 *
 * @author Pierre
 */
public class WldLazer implements Actor {

    final Body body;
    final static double lazerRadius = .1;
    final static double lazerLength = 5;

    WldLazer(Vector3 position, Matrix3 orient) {
        Geometry g = new UniformCapsule("beam", lazerRadius, lazerLength);
        body = new Body("lazer");
        body.addGeometry(orient, position, g);
        body.setVelocity(orient.multiply(Vector3.k().multiply(40.)));

    }

    public void spawn(Scene scene) {
        scene.addBody(body);
    }

    public Drawable optimize(GL3bc gl) {
        return this;
    }

    public void draw(RenderContext rc) {
        rc.applyLegacyMatrix();
        final GL3bc gl = rc.gl();
        gl.glMultMatrixd(body.getTransform().toArray(), 0);
        gl.glColor3f(1.f, 1.f, 0.f);
        gl.glBegin(gl.GL_LINE_LOOP);
        gl.glVertex3d(0., 0., lazerLength * .5);
        gl.glVertex3d(0., lazerRadius, lazerLength * .5 - lazerRadius);
        gl.glVertex3d(0., lazerRadius, -lazerLength * .5 + lazerRadius);
        gl.glVertex3d(0., 0., -lazerLength * .5);
        gl.glVertex3d(0., -lazerRadius, -lazerLength * .5 + lazerRadius);
        gl.glVertex3d(0., -lazerRadius, lazerLength * .5 - lazerRadius);
        gl.glEnd();
        gl.glBegin(gl.GL_LINE_LOOP);
        gl.glVertex3d(0., 0., lazerLength * .5);
        gl.glVertex3d(lazerRadius, 0., lazerLength * .5 - lazerRadius);
        gl.glVertex3d(lazerRadius, 0., -lazerLength * .5 + lazerRadius);
        gl.glVertex3d(0., 0., -lazerLength * .5);
        gl.glVertex3d(-lazerRadius, 0., -lazerLength * .5 + lazerRadius);
        gl.glVertex3d(-lazerRadius, 0., lazerLength * .5 - lazerRadius);
        gl.glEnd();

    }

    public void dispose(GL3bc gl) {
    }

    public void update(WldScene scene, double dt) {
    }
}
